<?php
/**
 * Created by PhpStorm.
 * User: Kevin
 * Date: 2019/6/21
 * Time: 10:44
 */
namespace app\admin\controller;
use think\Db;
class Hot extends Base{
    public function index(){
        if(request()->isAjax ()){
            extract (input ());
            $Nowpage = input('page') ? input('page'):1;
            $limits = input("limit")?input("limit"):10;// 获取总条数;
            $count = Db::name('hot_city h')
                ->join('area a','a.district_id = h.hot_city','left')
                ->join('area a1','a1.district_id = h.hot_qu','left')
                ->count();
            $lists =  Db::name('hot_city h')
                ->join('area a','a.district_id = h.hot_city','left')
                ->join('area a1','a1.district_id = h.hot_qu','left')
                ->field (['h.*','a.district','a1.district'=>'qu'])
                ->page($Nowpage,$limits)
                ->select ();
            return json(['code'=>220,'msg'=>'','count'=>$count,'data'=>$lists]);
        }
        return $this->fetch('hot/index');
    }


    public function hotAdd(){
        if(request ()->isPost ()){
            extract (input());
            $param = [
                'hot_city'=>$city,
                'hot_qu'=>$qu,
                'hot_add_time'=>date('Y-m-d H:i:s'),
                'hot_edit_time'=>date('Y-m-d H:i:s')
            ];
            $res = Db::name('hot_city')->insert($param);
            if($res){
                return json(['code'=>200,'msg'=>'添加成功']);
            }else{
                return json(['code'=>100,'msg'=>'添加失败']);
            }
        }
        $data = Db::name('area')->where('level',2)->select ();
        $have = Db::name('hot_city')->column ('hot_city');
        return $this->fetch ('hot/hot_add',['data'=>$data,'have'=>$have]);
    }

    public function getQuData(){
        $haveQu = Db::name('hot_city')->column ('hot_qu');
        $data = Db::name('area')
            ->where(['pid'=>input('id')])
            ->select ();
        return json(['code'=>200,'msg'=>$data,'data'=>$haveQu]);
    }


    public function hotEdit(){
        extract (input());
        if(request ()->isPost ()){
            $param = [
                'hot_city'=>$city,
                'hot_qu'=>$qu,
                'hot_edit_time'=>date('Y-m-d H:i:s')
            ];
            $res = Db::name('hot_city')->where('hot_id',$id)->update($param);
            if($res){
                return json(['code'=>200,'msg'=>'编辑成功']);
            }else{
                return json(['code'=>100,'msg'=>'编辑失败']);
            }
        }
        $data = Db::name('area')->where('level',2)->select ();
        $hot = Db::name('hot_city')->where('hot_id',$id)->find();
        $qu = Db::name('area')->where(['pid'=>$hot['hot_city']])->select ();
        $have = Db::name('hot_city')->where(['hot_qu'=>['neq',$hot['hot_qu']]])->column ('hot_qu');
        return $this->fetch ('hot/hot_edit',['data'=>$data,'have'=>$have,'hot'=>$hot,'qu'=>$qu]);
    }

    public function hotDel(){
        extract (input());
        $res = Db::name('hot_city')->where('hot_id',$id)->delete();
        if($res){
            return json(['code'=>200,'msg'=>'删除成功']);
        }else{
            return json(['code'=>100,'msg'=>'删除失败']);
        }
    }
}